package com.wzh.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wzh.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117677109";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDFXdGoSYO0pCsKsyTJla5i5cAHNTD+m1w/AjsI90pf7pbB8x03nZSStNIFNoR2MWkKo4MOmcCkRX/lTjUte41KO1J45qdMPK9GxuJFwxjMNQR4IYAgVyiMwL+VF1ySwLDgPvjPKnq26vj9Z4KPQoCtS+FL/YPFIT6KSd5nL+biuCyafJzxVqC7dDDFNSWudAqAI48dNYIofijB4RReRwOHs7h5B3X+f4Z+WBK/C8IjJogXe+qwVGCJ68k84y10BP6v/Qh1xxFbKwLdFujnxYLuSlvwqCz+eJKyJmWFe42l2AJRKKkwAaU27PMkZQSCCapfSPYDjxu229JK7zI3hXC/AgMBAAECggEAEAlQeLkIb4wLR0JJEewMkFKUqMFEqttawm/e9hqJu4O6UiX70MIICWeRzQSh/KyFGW0xv+T0RySygKI7JeftEgQB7nrnSwV/RGPyjLKOeEEyjMREEdyjGdSrfGS6i94GQSI8uzB7ahNricN4U0WXCP2qX69InqLV3wyjeoTk4LTrqtQu87Inn1ctpjtq5a620UVLOcEapYzdy/sBNsqcDJR3GyhrvOXfsT7g++U16BXV7XjeWPBP1z3jJ1zED9WarXsRmmrqgFH2P3UKwrbUjKpBez36aCoCUWKC+rohlkw5Pbyh6abOfAX+CNcjQ/WtgUQ5laiQPQDDCoxzcZerqQKBgQDusEW2njS+wft9aa7UkBiIbUcA1ERSzYPQU/9ZDrmyEWXxlrobQ9zdcerAM82NE8SyInw7UnyTXX6IFkr+96KHreaNdOUO3Ey7rTB5YsFKUP9zFTd41qwhffO8vKo1Ae0/ucKAdfNFzjt4YCjieA7nq4n0zWDCe1iqlLmgcwH5xQKBgQDTrlHtr5+uE9/SfY8DIsyOhp7+7DB7Cs/g369jPzcH8jd8MYEvNKsweGyjashuyLlhQ5A1Hu3UmTvcwgZyDPZqXD2oXOv3FyXh+AXdxH7MzmL8+grA1ygZKM/xjwaxzIyzBFflyuVbqtJcMuVBklLDhaeyCZu5c52wwkzvLO1cswKBgQC7KTNw7/sR/U+zVnRFrzvddjqv1sJAX3BVohffAGHx1Byo5HTIPl1osxPNZJFwZyjcOZhKd/LBJxW5pilmZk1msKDRGPEnxuzQWQxikmJYYMz9s4xMz6AnnvjJbPLWH82mI4X1bwe0G/Ob/2Lvug6N8H97mrOdrFSpZXds7eGFtQKBgHdDM62y0UiGsggp7bVamd2DQ3llITtxsAWGgY5tQ1gaw+AZFCvAhOLeNncr5plkRe3i3xDOyDiLzQpn1rHTU9JA/gwprliclh9mWgfEeZxye5sEVaKollL2h5IVfzQVhwAlZlB1rBuldrezhS8YfcDjQMQyXaZy2VmJX0cyDNm5AoGBAImAyonR4gvgo/hS10D3r9CJfq4O+7GmLvvZ7RhXCv5TvWAWkm+Uwn2mJfyPATrBHc2w4aeKROpNFJxZUyJXAjquw2MNnIPfcI9bmle8sPeuD/nBFHW5Zawmalg72qYmo4ovg9srJhSoRJvyziqRymNIiGzbj33BGS+f3PagJ5nL";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqygLFhu6mIXX+x/8krva0fHIF3kAE7+0gcUSxNXO2U0FdCT08D2ZsHatYHObpLJ8WOmTonNaKPnOzwgPzwmpJHv8BlX82Nb1j9/n60VCjvXoGJZHzuGo0Uh8Lsg6lXX1p5x5JC6GL9jKtW8/B3BDuPduyYbGwUN51/TkAIdRGDNXuMTTBAaFQLHaSOZ/f2jhQNykn3jSmWfk/Oe4cBcav1SMxOwPf6XWOxkp0MGUQSrY/bKXHSqpS0FZG0AzsHAL8yD4j2LoIEh6xrBqQy7NBDhIgVFEAVqvSSYxSU3KuAxnDBJ+wUwl+F6fU0ypmwiY+jTxJJSwx+HmTaqMjWYu0wIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://hig3eiab1i.cdhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
